#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "qpainter.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
    resize(480,480);
}

void MainWindow::paintEvent(QPaintEvent *e)
{
    DrawChessboard();
    update();
}

void MainWindow::DrawItems()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    for (int i = 0; i<Items.size(); i++)
    {
        Item item = Items[i];
        if (item.Black)
        {
            painter.setBrush(Qt::black);
        }
        else
        {
            painter.setBrush(Qt::white);
        }
        DrawChessAtPoint(painter,item.Pt);
    }
}

void MainWindow::DrawChessAtPoint(QPainter& painter,QPoint& pt)
{
    QPoint ptCenter((pt.x()+0.5)*30,(pt.y()+0.5)*30);
    painter.drawEllipse(ptCenter,12,12);
}
void MainWindow::DrawChessboard()
{
    QPainter painter(this);
    painter.setBrush(Qt::white);
    painter.setPen(QPen(QColor(Qt::black),2));

    for(int i = 0;i<15; i++)
    {
        for (int j = 0; j<15; j++)
        {
            painter.drawRect( (i+0.5)*30,(j+0.5)*30,30,30);
        }
    }
}
